﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SRW;
using SRW.BLL;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Text;

public partial class system_shiti_cxst : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PageData.Paging("wtproblem", "select * from wtproblem order by pkID desc", LabCount, LabPageCount, LabCurPage, HyLFirPage, HyLPrePage, HyLNextPage, HyLastPage, Repeater1,50);
            Panel1.Visible = true;
            Panel2.Visible = false;
            CheckBoxList1.DataTextField = "sinoname";
            CheckBoxList1.DataValueField = "sinoname";
            CheckBoxList1.DataSource = SQL.GetDataTable("sinoname", "select sinoname from sinopro");
            CheckBoxList1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedItem.Text == "选择题")
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        else
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //添加试题
        if (TextBox1.Text.Trim() == "")
        {
            MagicAjax.AjaxCallHelper.WriteAlert("请填写完整信息！");
            return;
        }
        Wtproblem mWtproblem = new Wtproblem();
        mWtproblem.wtproname = TextBox1.Text.Trim();
        mWtproblem.wtprotype = DropDownList1.SelectedItem.Text;
        if (bWtproblem.Insert(mWtproblem, false))
        {
            int problemID = SQL.GetNewId("wtproblem","pkID");
            List<string> SQLStringList = new List<string>();
            if (DropDownList1.SelectedItem.Text == "选择题")
            {
                if (TextBox2.Text.Trim() == "" || TextBox3.Text.Trim() == "" || TextBox4.Text.Trim() == "" || TextBox5.Text.Trim() == "" || TextBox6.Text.Trim()=="")
                {
                    MagicAjax.AjaxCallHelper.WriteAlert("请填写完整信息！");
                    bWtproblem.Delete(problemID);
                    return;
                }
                SQLStringList.Add("insert into wtanswer(wtanname,problemID) values('" + TextBox2.Text.Trim() + "'," + problemID + ")");
                SQLStringList.Add("insert into wtanswer(wtanname,problemID) values('" + TextBox3.Text.Trim() + "'," + problemID + ")");
                SQLStringList.Add("insert into wtanswer(wtanname,problemID) values('" + TextBox4.Text.Trim() + "'," + problemID + ")");
                SQLStringList.Add("insert into wtanswer(wtanname,problemID) values('" + TextBox5.Text.Trim() + "'," + problemID + ")");
                SQLStringList.Add("insert into rightan(rightname,problemID) values('" + TextBox6.Text.Trim() + "'," + problemID + ")");
            }
            else if (DropDownList1.SelectedItem.Text == "判断题")
            {
                if (TextBox7.Text.Trim() == "")
                {
                    MagicAjax.AjaxCallHelper.WriteAlert("请填写完整信息！");
                    bWtproblem.Delete(problemID);
                    return;
                }
                SQLStringList.Add("insert into wtanswer(wtanname,problemID) values('正确'," + problemID + ")");
                SQLStringList.Add("insert into wtanswer(wtanname,problemID) values('错误'," + problemID + ")");
                SQLStringList.Add("insert into rightan(rightname,problemID) values('" + TextBox7.Text.Trim() + "'," + problemID + ")");
            }
            if (SQL.ExecuteSqlTran(SQLStringList))
            {
                List<string> mCheckList = BindCheckBoxList.GetCheckBoxInfo(CheckBoxList1);
                List<string> SQLsinoList = new List<string>();
                for (int i = 0; i < mCheckList.Count; i++)
                {
                    int sinoID = int.Parse(SQL.GetData("select pkID from sinopro where sinoname='" + mCheckList[i] + "'", "pkID"));
                    SQLsinoList.Add("insert into problemsino(problemID,sinoID) values(" + problemID + "," + sinoID + ")");
                }
                if (SQLsinoList.Count == 0)
                {
                    MagicAjax.AjaxCallHelper.WriteAlert("请填写完整信息！");
                    SQL.ExecScalarSQL("delete from wtanswer where problemID=" + problemID + "");
                    SQL.ExecScalarSQL("delete from rightan where problemID=" + problemID + "");
                    bWtproblem.Delete(problemID);//回滚数据
                    return;
                }
                if (SQL.ExecuteSqlTran(SQLsinoList))
                {
                    MagicAjax.AjaxCallHelper.WriteAlert("试题添加成功！");
                    PageData.Paging("wtproblem", "select * from wtproblem order by pkID desc", LabCount, LabPageCount, LabCurPage, HyLFirPage, HyLPrePage, HyLNextPage, HyLastPage, Repeater1, 15);
                }
                else
                {
                    SQL.ExecScalarSQL("delete from wtanswer where problemID=" + problemID + "");
                    SQL.ExecScalarSQL("delete from rightan where problemID=" + problemID + "");
                    bWtproblem.Delete(problemID);//回滚数据
                    MagicAjax.AjaxCallHelper.WriteAlert("试题添加失败！");
                }
            }
            else
            {
                bWtproblem.Delete(problemID);//回滚数据
                MagicAjax.AjaxCallHelper.WriteAlert("试题添加失败！");
            }
        }
        else
        {
            MagicAjax.AjaxCallHelper.WriteAlert("试题添加失败！");
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        //批量导入试题
        HttpPostedFile httpup = FileUpload1.PostedFile;
        string filename = "";
        if (httpup.ContentLength != 0)
        {
            filename = httpup.FileName;
        }
        string savePath = filename;//Server.MapPath 获得虚拟服务器相对路径
        DataSet ds;

        ds = ExecleDs(savePath, filename);
        DataRow[] dr = ds.Tables[0].Select();            //定义一个DataRow数组
        int rowsnum = ds.Tables[0].Rows.Count;
        if (rowsnum == 0)
        {
            //MessageBox.Show("表为空表,无数据!");  //当表为空时,对用户进行提示
        }
        else
        {
            List<string> SQLStringList = new List<string>();
            List<string> StrListID = new List<string>();
            for (int i = 0; i < dr.Length; i++)
            {
                StringBuilder mProBList = new StringBuilder();
                StringBuilder mAnswerA = new StringBuilder();
                StringBuilder mAnswerB = new StringBuilder();
                StringBuilder mAnswerC = new StringBuilder();
                StringBuilder mAnswerD = new StringBuilder();
                StringBuilder mRightan = new StringBuilder();
                StringBuilder mProSino = new StringBuilder();


                mProBList.Append("insert into wtproblem(wtproname,wtprotype) values('" + dr[i]["题目"].ToString() + "','" + dr[i]["题目类型"].ToString() + "')");
                int SQLCount = SQL.ExecScalarSQL(mProBList.ToString());
                if (SQLCount > 0 && dr[i]["题目类型"].ToString() == "选择题")
                {
                    int GetNowId = SQL.GetNewId("wtproblem", "pkID");
                    StrListID.Add(GetNowId.ToString());//预留，用作数据回滚
                    mAnswerA.Append("insert into wtanswer(wtanname,problemID) values('" + dr[i]["答案A"].ToString() + "'," + GetNowId + ")");
                    mAnswerB.Append("insert into wtanswer(wtanname,problemID) values('" + dr[i]["答案B"].ToString() + "'," + GetNowId + ")");
                    mAnswerC.Append("insert into wtanswer(wtanname,problemID) values('" + dr[i]["答案C"].ToString() + "'," + GetNowId + ")");
                    if (!(string.IsNullOrEmpty(dr[i]["答案D"].ToString())))
                    {
                        mAnswerD.Append("insert into wtanswer(wtanname,problemID) values('" + dr[i]["答案D"].ToString() + "'," + GetNowId + ")");
                    }
                    mRightan.Append("insert into rightan(rightname,problemID) values('" + dr[i]["正确答案"].ToString() + "'," + GetNowId + ")");
                    int sinoID = int.Parse(SQL.GetData("select pkID from sinopro where sinoname='" + dr[i]["题目所属专项"].ToString() + "'", "pkID"));
                    mProSino.Append("insert into problemsino(problemID,sinoID) values(" + GetNowId + "," + sinoID + ")");
                    SQLStringList.Add(mAnswerA.ToString());
                    SQLStringList.Add(mAnswerB.ToString());
                    SQLStringList.Add(mAnswerC.ToString());
                    SQLStringList.Add(mAnswerD.ToString());
                    SQLStringList.Add(mRightan.ToString());
                    SQLStringList.Add(mProSino.ToString());
                }
                else if (SQLCount > 0 && dr[i]["题目类型"].ToString() == "判断题")
                {
                    int GetNowId = SQL.GetNewId("wtproblem", "pkID");
                    StrListID.Add(GetNowId.ToString());//预留，用作数据回滚
                    mAnswerA.Append("insert into wtanswer(wtanname,problemID) values('正确'," + GetNowId + ")");
                    mAnswerB.Append("insert into wtanswer(wtanname,problemID) values('错误'," + GetNowId + ")");
                    mRightan.Append("insert into rightan(rightname,problemID) values('" + dr[i]["正确答案"].ToString() + "'," + GetNowId + ")");
                    int sinoID = int.Parse(SQL.GetData("select pkID from sinopro where sinoname='" + dr[i]["题目所属专项"].ToString() + "'", "pkID"));
                    mProSino.Append("insert into problemsino(problemID,sinoID) values(" + GetNowId + "," + sinoID + ")");
                    SQLStringList.Add(mAnswerA.ToString());
                    SQLStringList.Add(mAnswerB.ToString());
                    SQLStringList.Add(mRightan.ToString());
                    SQLStringList.Add(mProSino.ToString());
                }
                
            }
            try
            {
                if (SQL.ExecuteSqlTran(SQLStringList))
                {
                    MagicAjax.AjaxCallHelper.WriteAlert("试题导入成功！");
                    PageData.Paging("wtproblem", "select * from wtproblem order by pkID desc", LabCount, LabPageCount, LabCurPage, HyLFirPage, HyLPrePage, HyLNextPage, HyLastPage, Repeater1, 15);
                }
            }
            catch
            {
                List<string> StrDelete = new List<string>();
                for (int i = 0; i < StrListID.Count; i++)
                {
                    StrDelete.Add("delete from wtproblem where pkID=" + int.Parse(StrDelete[i].ToString()) + "");
                }
                if (SQL.ExecuteSqlTran(StrDelete))
                {
                    MagicAjax.AjaxCallHelper.WriteAlert("题目导入失败，正在进行数据修复！");
                }
            }
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        //全选
        CheckBox2.Checked = true;
        CheckBox3.Checked = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (!CheckBox1.Checked)
            {
                CheckBox1.Checked = true;
            }
        }
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        //反选
        CheckBox3.Checked = true;
        CheckBox2.Checked = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (CheckBox1.Checked)
            {
                CheckBox1.Checked = false;
            }
            else
            {
                CheckBox1.Checked = true;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //删除
        bool panduan = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (CheckBox1.Checked)
            {
                Label Label1 = (Label)item.FindControl("Label1");
                bWtproblem.Delete(int.Parse(Label1.Text));
                try
                {
                    SQL.ExecScalarSQL("delete from wtanswer where problemID=" + int.Parse(Label1.Text) + "");
                    SQL.ExecScalarSQL("delete from rightan where problemID=" + int.Parse(Label1.Text) + "");
                    SQL.ExecScalarSQL("delete from problemsino where problemID=" + int.Parse(Label1.Text) + "");

                    //获得试卷名
                    string papername = SQL.GetData("select papername from wtpaper where problemID=" + int.Parse(Label1.Text) + "", "papername");
                    if (!(string.IsNullOrEmpty(papername)))
                    {
                        SQL.ExecScalarSQL("delete from wtpaper where papername='" + papername + "'");
                    }
                }
                catch(Exception ex)
                {
                    panduan = true;
                    break;
                }
            }
        }
        if (!panduan)
        {
            MagicAjax.AjaxCallHelper.WriteAlert("删除成功！");
            PageData.Paging("wtproblem", "select * from wtproblem order by pkID desc", LabCount, LabPageCount, LabCurPage, HyLFirPage, HyLPrePage, HyLNextPage, HyLastPage, Repeater1, 15);
        }
        else
        {
            MagicAjax.AjaxCallHelper.WriteAlert("系统出现错误！");
        }
    }
    public DataSet ExecleDs(string filenameurl, string table)
    {
        string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        DataSet ds = new DataSet();
        OleDbDataAdapter odda;
        odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
        odda.Fill(ds, table);
        return ds;
    }
}
